package leetcode;

public class lc_279 {
    public static void main(String[] args) {
        System.out.println(numSquares(5));
    }
    public static int numSquares(int n) {
        int t = (int) Math.sqrt(n);
        int[] dp = new int[n + 1];
        for (int i = 1; i < n + 1; i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        for(int i = 1; i <= t; i++){
            for (int j = i * i; j <= n; j++) {
                if(dp[j - i * i] != Integer.MAX_VALUE)
                    dp[j] = Math.min(dp[j], dp[j - i * i] + 1);
            }
        }
        return dp[n];
    }
}
